﻿using Autofac;
using Codeflows;
using Codeflows.Configuration;
using Codeflows.Contrib.Autofac;
using Codeflows.Contrib.Log4Net;
using Codeflows.Tracking;
using Codeflows.Tracking.BuiltIn;
using log4net.Config;

namespace Cinderella
{
    public class ConsumerCodeflowsHostConfiguration : IConfigureCodeflowHosts
    {
        public void Configure()
        {
            var container = SetupContainer(new ContainerBuilder());

            // Init log4net.
            XmlConfigurator.Configure();

            // Override codeflow factory impl to use the autofac ones.
            CodeflowApplication.SetCodeflowTrackerFactory(new AutofacCodeflowTrackerFactory(container));
            CodeflowApplication.SetCodeflowFactory(new AutofacCodeflowFactory(container));
            CodeflowApplication.SetLogProvider(new Log4NetLogProvider());
        }

        private static IContainer SetupContainer(ContainerBuilder builder)
        {
            // Register the sql tracker we are using, the module does not do this so we can customise the
            // tracker registration.
            builder.RegisterType<SqlServerTracker>().As<ICodeflowTracker>().InstancePerLifetimeScope();

            // Register our codeflow instances.
            builder.RegisterType<CinderellaWorm>();

            return builder.Build();
        }
    }
}
